package kr.or.javacafe.manage.controller;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;


import kr.or.javacafe.code.bo.CodeCategoryBO;
import kr.or.javacafe.manage.bo.*;
import kr.or.javacafe.manage.domain.*;
import kr.or.javacafe.member.bo.MemberBO;
import kr.or.javacafe.member.domain.User;
import kr.or.javacafe.member.domain.UserTeam;

@Controller
@RequestMapping("/manage")
public class ManageController {
	
	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ManageController.class);
		
	@Autowired
	CodeCategoryBO codeBO;
	
	@Autowired
	ManageBO manageBO;
	
	@Autowired
	MemberBO memberBO;
	
	

	
	@RequestMapping(value = "/memberRole/list", method = RequestMethod.GET)
	public String memberRole(Model model) {
				
		User param = new User();

		int intListCount = memberBO.getUserListCount(param);
		
		param.setStartNum(0);
		param.setEndNum(100);
		
		List<User> objList = memberBO.getUserList(param);
		
		model.addAttribute("objList", objList);
		
		return "manage/memberRole";
	}
	
	@RequestMapping(value = "/memberRole/view/{userId}", method = RequestMethod.GET)
	public String memberRoleEdit(@PathVariable String userId, Model model) {
		User param = new User();
		param.setId(userId);
		
		model.addAttribute("user", memberBO.viewUser(param));
		model.addAttribute("roleGrpCodeList", codeBO.listRoleGrpForCombo());
		
		return "manage/memberRoleEdit";
	}
	
	@RequestMapping(value = "/memberRole/view/{userId}", method = RequestMethod.POST)
	public String memberRoleEdit(@ModelAttribute("user") User user) {
		memberBO.editUserRole(user);
				
		return "redirect:/manage/memberRole/list";
	}	

	
	

	@RequestMapping(value = "/memberTeam/list", method = RequestMethod.GET)
	public String memberTeam(Model model) {
				
		User param = new User();

		int intListCount = memberBO.getUserListCount(param);
		
		param.setStartNum(0);
		param.setEndNum(100);
		
		List<User> objList = memberBO.getUserList(param);
		
		model.addAttribute("objList", objList);
		
		return "manage/memberTeam";
	}

	@RequestMapping(value = "/memberTeam/view/{userId}", method = RequestMethod.GET)
	public String memberTeamEdit(@PathVariable String userId, Model model) {
		User param = new User();
		param.setId(userId);
		
		model.addAttribute("user", memberBO.viewUser(param));
		model.addAttribute("teamCodeList", codeBO.listCodeCategoryForCombo("TEAM_CD"));
		
		return "manage/memberTeamEdit";
	}
	
	@RequestMapping(value = "/memberTeam/view/{userId}", method = RequestMethod.POST)
	public String memberTeamEdit(@ModelAttribute("user") User user) {
		UserTeam ut = new UserTeam();
		ut.setId(user.getId());
		ut.setTeamCd(user.getUserTeam().getTeamCd());
		
		memberBO.editUserTeam(ut);

		return "redirect:/manage/memberTeam/list";
	}	

	
	
	
	@RequestMapping(value = "/memberHistory/list", method = RequestMethod.GET)
	public String memberHistory(Model model) {
				
		User param = new User();

		int intListCount = memberBO.getUserListCount(param);

		param.setStartNum(0);
		param.setEndNum(100);
		
		List<User> objList = memberBO.getUserList(param);
		
		model.addAttribute("objList", objList);
		
		return "manage/memberHistory";
	}


	
	
	
	@RequestMapping(value = "/managerInfo/list", method = RequestMethod.GET)
	public String managerInfo(Model model) {
				
		User param = new User();
		param.setRoleGrpId("G3");

		int intListCount = memberBO.getUserListCount(param);

		param.setStartNum(0);
		param.setEndNum(100);
		
		List<User> objList = memberBO.getUserList(param);
		
		model.addAttribute("objList", objList);
		
		return "manage/managerInfo";
	}

	@RequestMapping(value = "/managerInfo/view/{userId}", method = RequestMethod.GET)
	public String managerInfoEdit(@PathVariable String userId, Model model) {
		User param = new User();
		param.setId(userId);
		
		model.addAttribute("user", memberBO.viewUser(param));
		
		return "manage/managerInfoEdit";
	}
	
	@RequestMapping(value = "/managerInfo/view/{userId}", method = RequestMethod.POST)
	public String managerInfoEdit(@ModelAttribute("user") User user) {
		UserTeam ut = new UserTeam();
		ut.setId(user.getId());
		ut.setPosDesc(user.getUserTeam().getPosDesc());
		ut.setIntro(user.getUserTeam().getIntro());
		
		memberBO.editUserTeamIntroduction(ut);
		
		return "redirect:/manage/managerInfo/list";
	}	

	
	
	
	
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String welcome(Model model) {
		model.addAttribute("information", manageBO.getWelcome());
		
		return "/manage/welcome";
	}

	@RequestMapping(value = "/welcome/text", method = RequestMethod.GET)
	@ResponseBody
	public Information welcomeText() {
		return manageBO.getWelcome();
	}
	
	@RequestMapping(value = "/welcome/edit", method = RequestMethod.GET)
	public String welcomeEdit(Model model) {		
		return "/manage/welcomeEdit";
	}

	@RequestMapping(value = "/welcome/edit", method = RequestMethod.POST)
	public String welcomeEdit(@Valid Information info, BindingResult result, SessionStatus status) {
		if (result.hasErrors()) {
			return "/manage/welcomeEdit";
		}
		manageBO.editWelcome(info);
		
		return "redirect:/intro/welcome";
	}
	
	
	
	
	@RequestMapping(value = "/history", method = RequestMethod.GET)
	public String history(Model model) {
		model.addAttribute("information", manageBO.getHistory());
		
		return "/manage/history";
	}

	@RequestMapping(value = "/history/text", method = RequestMethod.GET)
	@ResponseBody
	public Information historyText() {
		return manageBO.getHistory();
	}
	
	@RequestMapping(value = "/history/edit", method = RequestMethod.GET)
	public String historyEdit(Model model) {		
		return "/manage/historyEdit";
	}

	@RequestMapping(value = "/history/edit", method = RequestMethod.POST)
	public String historyEdit(@Valid Information info, BindingResult result, SessionStatus status) {
		if (result.hasErrors()) {
			return "/manage/historyEdit";
		}
		manageBO.editHistory(info);
		
		return "redirect:/intro/history";
	}
	
	

	
	
	@RequestMapping(value = "/regulation", method = RequestMethod.GET)
	public String regulation(Model model) {
		model.addAttribute("information", manageBO.getRegulation());
		
		return "/manage/regulation";
	}

	@RequestMapping(value = "/regulation/text", method = RequestMethod.GET)
	@ResponseBody
	public Information regulationText() {
		return manageBO.getRegulation();
	}
	
	@RequestMapping(value = "/regulation/edit", method = RequestMethod.GET)
	public String regulationEdit(Model model) {		
		return "/manage/regulationEdit";
	}

	@RequestMapping(value = "/regulation/edit", method = RequestMethod.POST)
	public String regulationEdit(@Valid Information info, BindingResult result, SessionStatus status) {
		if (result.hasErrors()) {
			return "/manage/regulationEdit";
		}
		manageBO.editRegulation(info);
		
		return "redirect:/intro/regulation";
	}
	

	
	

}






